<template>
  <div class="home">
    <div class="swiper">
      <van-swipe :autoplay="3000">
        <van-swipe-item v-for="(image, index) in images" :key="index" style="background-color: #fff;">
          <img v-lazy="image" />
        </van-swipe-item>
      </van-swipe>
    </div>

    <!-- <div class="grid" @click="$router.push('/category')">
      <van-grid>
        <van-grid-item>
          <img src="../assets/img/banner.jpg" style="border-radius: 50;" />
          节
        </van-grid-item>
        <van-grid-item>
          <img src="../assets/img/banner.jpg" style="border-radius: 50;" />
          点
        </van-grid-item>
        <van-grid-item>
          <img src="../assets/img/banner.jpg" style="border-radius: 50;" />
          飞
        </van-grid-item>
        <van-grid-item>
          <img src="../assets/img/banner.jpg" style="border-radius: 50;" />
          思
        </van-grid-item>
      </van-grid>
    </div> -->

    <!-- <img src="../assets/img/banner.jpg" alt="" /> -->

    <van-sticky :offset-top="40">
      <span class="title">推荐商品</span>
    </van-sticky>

    <van-card v-for="(item,index) in recomendList" :tag="item.type"
      @click="$router.push({path:'/productDetail',query:{productId:item.id}})" :key="index" :price="item.price"
      :desc="item.intro" :title="item.name" :thumb="$baseUrl + item.cover.slice(2)" />

    <van-sticky :offset-top="40">
      <span class="title">周边商品</span>
    </van-sticky>

    <product-list-vue :productList="productList"></product-list-vue>
    <div class="button" @click="getMore" v-show="nothing">点击加载更多</div>
  </div>
</template>

<script>
// @ is an alias to /src
import { getProductList } from '@/request/api.js'
import ProductListVue from '../components/product/ProductList.vue';
export default {
  name: 'Home',
  components: {
    ProductListVue
  },
  data() {
    return {
      images: [
        require('../assets/img/banner.jpg'),
        require('../assets/img/banner.jpg'),
        require('../assets/img/banner.jpg'),
        require('../assets/img/banner.jpg'),
        require('../assets/img/banner.jpg')
      ],
      productList: [],
      recomendList: [],
      length: 10,
      nothing: true
    };
  },
  mounted() {
    this.getAll()
  },
  methods: {
    getAll() {
      getProductList().then(res => {
        this.productList = res.data.records.slice(-this.length)
        this.recomendList = res.data.records.slice(0, 5)
        console.log(this.recomendList);
      })
    },
    getMore() {
      if (this.length<0) {
        this.nothing = false
        this.$toast('没有更多商品了')
        return
      }
      this.length -= 10
      this.getAll()
    }
  }
}
</script>

<style scoped>
.button {
  height: 40px;
  width: 120px;
  background: #fc4a1a;
  color: #fff;
  text-align: center;
  line-height: 40px;
  margin: 0 auto;
}

.van-card__price {
  color: orangered;
}

.title {
  color: #fff;
  box-sizing: border-box;
  font-weight: 700;
  margin: 5px 0;
  width: 100%;
  padding-bottom: 10px;
  display: inline-block;
  border-bottom: 2px solid orangered;
  background: #f18162;
}

.van-grid-item {
  font-size: 12px;
}

::v-deep .van-image img {
  border-radius: 50%;
}

.home {
  padding: 45px 5px 55px 5px;
  background: #f18162;
}

.swiper {
  height: 250px;
  overflow: hidden;
}

.van-swipe {
  height: 100%;
}

img {
  width: 100%;
  height: 100%;
}

.grid {
  margin: 5px 0;
}
</style>
